/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.assg2.dao;

import com.movie.Movie;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author Kevin
 */
public class DaoMovie extends GenericDao {

    public DaoMovie() {
        super(Movie.class);
    }

    public List<Movie> listShowing() throws ParseException {

        List<Movie> showing = new ArrayList<Movie>();

        for (int i = 0; i < listall().size(); i++) {
            Movie movie = (Movie) listall().get(i);
            if (movie.Showing()) {
                showing.add((Movie) listall().get(i));
            }
        }

        return showing;
    }

    public List<Movie> listComing() throws ParseException {

        List<Movie> coming = new ArrayList<Movie>();

        for (int i = 0; i < listall().size(); i++) {
            Movie movie = (Movie) listall().get(i);
            if (movie.Coming()) {
                coming.add((Movie) listall().get(i));
            }
        }

        return coming;
    }

    public Double movieratingavg(int id) {
        Query q = query("select avg(r.rating) from Review r, Movie m where r.movieIdmovie = m.idmovie and m.idmovie=" + id);
        if (q.list().get(0) != null) {
            return Double.parseDouble(q.list().get(0).toString());
        } else {
            return Double.parseDouble("0");
        }
    }

    public int numberreviews(int id) {
        Query q = query("select count(r.rating) from Review r, Movie m where r.movieIdmovie = m.idmovie and m.idmovie=" + id);
        if (q.list().get(0) != null) {
            return Integer.parseInt(q.list().get(0).toString());
        } else {
            return 0;
        }
    }

    public String movierating(int id) {
        int numreviews = numberreviews(id);
        Double avg = movieratingavg(id);
        if (numreviews == 0) {
            return "No Reviews Yet";
        } else {
            return avg.toString();
        }
    }

    public List<Movie> recomendedmovies(int id) {
        Query q = query("select distinct m2 from Review r, Viewer v , Movie m, Movie m2 where v.idviewer=r.viewerIdviewer and r.movieIdmovie = m.idmovie and r.rating>2 and m.genreIdgenre=m2.genreIdgenre and v.idviewer=" + id + " and m2.until>:showDate").setParameter("showDate", new java.util.Date());
        //System.out.println(q.list().size());
        if (q.list().get(0) != null) {
            return q.list();
        } else {
            return new ArrayList<Movie>();
        }
    }

    public String starreviews(int id) {
        int avg = (int) Math.round(movieratingavg(id));
        String reviewsavg = null;
        if (avg == 0) {
            reviewsavg = "<ul class='rating nostar'>";
        }
        if (avg == 1) {
            reviewsavg = "<ul class='rating onestar'>";
        }
        if (avg == 2) {
            reviewsavg = "<ul class='rating twostar'>";
        }
        if (avg == 3) {
            reviewsavg = "<ul class='rating threestar'>";
        }
        if (avg == 4) {
            reviewsavg = "<ul class='rating fourstar'>";
        }
        if (avg == 5) {
            reviewsavg = "<ul class='rating fivestar'>";
        }

        reviewsavg = reviewsavg + "<li class='one'><a href='dispatcher?operation=review&movieid=" + Integer.toString(id) + "&review=1' title='1 Star'>1</a></li>";
        reviewsavg = reviewsavg + "<li class='two'><a href='dispatcher?operation=review&movieid=" + Integer.toString(id) + "&review=2' title='2 Stars'>2</a></li>";
        reviewsavg = reviewsavg + "<li class='three'><a href='dispatcher?operation=review&movieid=" + Integer.toString(id) + "&review=3' title='3 Stars'>3</a></li>";
        reviewsavg = reviewsavg + "<li class='four'><a href='dispatcher?operation=review&movieid=" + Integer.toString(id) + "&review=4' title='4 Stars'>4</a></li>";
        reviewsavg = reviewsavg + "<li class='five'><a href='dispatcher?operation=review&movieid=" + Integer.toString(id) + "&review=5' title='5 Stars'>5</a></li>";
        reviewsavg = reviewsavg + "</ul>";

        return reviewsavg;

    }
}
